<template>
  <div>
    <!-- 头部组件 -->
    <con-title :info="Toptitle"></con-title>
    <!-- 表格 -->
    <div class="tab">
      <div class="search">
        <el-tabs type="border-card" activeName>
          <el-tab-pane label="所有设备"></el-tab-pane>
          <el-tab-pane label="LoRaWAN"></el-tab-pane>
          <el-tab-pane label="NB-loT"></el-tab-pane>
          <el-tab-pane label="4G/2G"></el-tab-pane>
          <el-tab-pane label="LoRaPP"></el-tab-pane>
          <el-form ref="form" :model="query" label-width="80px" size="medium">
            <el-form-item label="EUI" label-width="80px" width="300px">
              <el-input
                v-model="query.eui"
                placeholder="EUI"
                :style="{ width: '230px' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="设备群组">
              <el-input
                v-model="query.devGroup"
                placeholder="设备群组"
                :style="{ width: '230px' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="在线状态">
              <el-select v-model="query.online" placeholder="在线状态">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="绑定时间" size="mini">
              <el-date-picker
                v-model="query.date"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item size="large">
              <el-button type="primary" icon="el-icon-search" @click="onSubmit"
                >搜索</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-delete"
                @click="handleClean"
                >清除条件</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-refresh"
                @click="handleRefresh"
                >刷新</el-button
              >
              <span style="margin-left:20px">共有记录:{{total}}</span>
            </el-form-item>
          </el-form>
        </el-tabs>
      </div>
      <el-table :data="nodelist" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="eui" label="EUI" width="120" align="center"> </el-table-column>
        <el-table-column prop="name" label="设备名称" width="150" align="center">
        </el-table-column>
        <el-table-column prop="nums" label="传感器通道" align="center"> </el-table-column>
        <el-table-column prop="devGroup" label="设备群组" width="120" align="center">
        </el-table-column>
        <el-table-column prop="loop" label="上报周期(分钟)" width="120" align="center">
        </el-table-column>
        <el-table-column prop="mhz" label="频率(MHz)" algin="center"> </el-table-column>
        <el-table-column label="在线状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.online | onlineType }}
          </template>
        </el-table-column>
        <el-table-column prop="bTime" label="绑定时间" width="130" align="center">
            <template slot-scope="scope">
            {{ scope.row.bTime | format }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100" algin="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button type="text" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currPage"
          :page-sizes="[5, 15, 20, 30]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// 引入后台数据
import { $nodeList } from "@/api/index.js";
// 引入组件
import ConTitle from "@/components/ConTitle.vue";
// import Search from "../../components/search.vue";
export default {
  name: "sensorlist",
  components: { ConTitle },
  data() {
    return {
      nodelist: [],
      Toptitle: {
        type: "设备/",
        text: "传感器列表",
        msg: "-展示EUI、名称、状态、数据类型登设备信息、可根据EUI、群组、状态、绑定时间等条件筛选具体的传感器节点，并提供移动群组等功能。",
      },
      activeName: "all", //默认打开tab状态
      total: 0,
      query: {
        devGroup:"",
        currPage: 1,
        pageSize: 5,
        eui: "",
        online: "all",
        agreement: "",
        date: [],
      },
      options: [
        { value: true, label: "在线" },
        { value: false, label: "离线" },
        { value: "all", label: "全部" },
      ],
    };
  },
   computed: {
    // 每次修改时间时，默认把时间段变成有 - 的字符串
    dateStr() {
      let d1 = this.query.date[0];
      let d2 = this.query.date[1];
      console.log(d1, d2);
      let time1 = d1 && d1.getTime();
      let time2 = d2 && d2.getTime();
      console.log(time1, time2);
      if (time1 && time2) {
        return time1 + "-" + time2;
      } else {
        return "";
      }
    },
  },
  filters: {
    onlineType(value) {
      return value ? "在线" : "离线";
    },
    format(value) {
      // 数字小于10则加0
      function addZero(num) {
        return num >= 10 ? num : "0" + num;
      }

      var time = new Date(value);
      var y = time.getFullYear();
      var m = addZero(time.getMonth() + 1);
      var d = addZero(time.getDate());
      return `${y}-${m}-${d}`;
    },
  },
  created() {
    this.getNodeList();
  },
  methods: {
       //切换tab栏
    handleChangeTab(value) {
      this.query.agreement = value.name;
      // 提交数据
      this.getNodeList();
    },
    // 清除条件
    handleClean() {
      this.query = {
        keyword: "",
        pageNum: 1,
        pageSize: 5,
        eui: "",
        online: "all",
        agreement: "all",
        date: [],
      };
      this.getNodeList();
    },
    // 刷新页面
    handleRefresh() {
      this.$router.go(0);
    },
     // 提交筛选数据
    onSubmit() {
      console.log("表单提交了", this.query);

      this.getNodeList();
    },
    getNodeList() {
          var queryStr = { ...this.query };
      //需要将 搜索的数据重新赋值
      if (this.dataStr != "") {
        queryStr.date = this.dateStr;
      }
      $nodeList(queryStr).then((res) => {
        console.log(res.data.data);
        this.nodelist = res.data.data;
        console.log(this.nodelist);
        this.total = res.data.total;
      });
    },
    handleSizeChange(num) {
      console.log("显示条数", num);
      this.query.pageSize = num;
      this.query.pageNum = 1;

      // 重新获取数据
      this.getNodeList();
    },
    handleCurrentChange(num) {
      console.log("当前页", num);
      // 将当前页赋值个currpage
      this.query.pageNum = num;
      // 重新获取数据
      this.getNodeList();
    },
  },
};
</script>

<style lang="scss" scoped>
.tab {
  margin-top: 20px;
  .paging {
    width: 100%;
    height: 50px;
    background: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>